package com.opencloud.system.client.service;

import com.opencloud.common.model.Result;
import com.opencloud.common.security.XAuthority;
import com.opencloud.system.client.model.AuthorityApi;
import com.opencloud.system.client.model.AuthorityMenu;
import com.opencloud.system.client.model.AuthorityResource;
import com.opencloud.system.client.model.entity.SystemAuthorityAction;
import io.swagger.annotations.ApiOperation;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 权限控制API接口
 *
 * @author liuyadu
 */
public interface ISystemAuthorityServiceClient {
    /**
     * 获取所有访问权限列表
     */
    @GetMapping("/authority/access")
    Result<List<AuthorityResource>> findAuthorityResource();

    /**
     * 获取接口权限列表
     *
     * @param appId
     * @return
     */
    @GetMapping("/authority/api")
    Result<List<AuthorityApi>> findAuthorityApi(
            @RequestParam(value = "appId", required = false) String appId
    );

    /**
     * 获取菜单权限列表
     *
     * @return
     */
    @GetMapping("/authority/menu")
    Result<List<AuthorityMenu>> findAuthorityMenu();

    /**
     * 获取功能权限列表
     *
     * @param actionId
     * @return
     */
    @GetMapping("/authority/action")
    Result<List<SystemAuthorityAction>> findAuthorityAction(@RequestParam(value = "actionId") Long actionId);

    /**
     * 获取角色已分配权限
     *
     * @param roleId
     * @return
     */
    @GetMapping("/authority/role")
    Result<List<XAuthority>> findAuthorityRole(@RequestParam(value = "roleId") Long roleId);


    /**
     * 获取用户菜单列表
     * @param userId
     * @param root
     * @return
     */
    @GetMapping("/authority/user/menu")
    Result<List<AuthorityMenu>> findAuthorityMenuByUser(@RequestParam(value = "userId") Long userId,@RequestParam(value = "root")  Boolean root);

    /**
     * 获取用户已分配权限
     *
     * @param userId
     * @return
     */
    @GetMapping("/authority/user")
    Result<List<XAuthority>> findAuthorityUser(@RequestParam(value = "userId") Long userId);

    /**
     * 获取应用已分配接口权限
     *
     * @param appId
     * @return
     */
    @GetMapping("/authority/app")
    Result<List<XAuthority>> findAuthorityApp(@RequestParam(value = "appId") String appId);

    /**
     * 分配角色权限
     *
     * @param roleId
     * @param expireTime
     * @param authorityIds
     * @return
     */
    @PostMapping("/authority/role/grant")
    Result grantAuthorityRole(
            @RequestParam(value = "roleId") Long roleId,
            @RequestParam(value = "expireTime", required = false) @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date expireTime,
            @RequestParam(value = "authorityIds", required = false) String authorityIds
    );

    /**
     * 分配角色权限
     *
     * @param roleId       角色ID
     * @param expireTime   授权过期时间
     * @param authorityIds 权限ID.多个以,隔开
     * @param appId
     * @return
     */
    @PostMapping("/authority/role/granta")
    Result grantAuthorityRoleApp(
            @RequestParam(value = "roleId") Long roleId,
            @RequestParam(value = "expireTime", required = false) @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date expireTime,
            @RequestParam(value = "authorityIds", required = false) String authorityIds,
            @RequestParam(value = "appId") Long appId
    );

    /**
     * 分配用户权限
     *
     * @param userId
     * @param expireTime
     * @param authorityIds
     * @return
     */
    @PostMapping("/authority/user/grant")
    Result grantAuthorityUser(
            @RequestParam(value = "userId") Long userId,
            @RequestParam(value = "expireTime", required = false) @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date expireTime,
            @RequestParam(value = "authorityIds", required = false) String authorityIds
    );

    /**
     * 分配用户权限
     * @param userId
     * @param expireTime
     * @param authorityIds
     * @param appId
     * @return
     */
    @ApiOperation(value = "分配用户权限", notes = "分配用户权限")
    @PostMapping("/authority/user/granta")
    Result grantAuthorityUserApp(
            @RequestParam(value = "userId") Long userId,
            @RequestParam(value = "expireTime", required = false) @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date expireTime,
            @RequestParam(value = "authorityIds", required = false) String authorityIds,
            @RequestParam(value = "appId") Long appId
    );

    /**
     * 分配应用权限
     *
     * @param appId
     * @param expireTime
     * @param authorityIds
     * @return
     */
    @PostMapping("/authority/app/grant")
    Result grantAuthorityApp(@RequestBody Map<String, Object> map);

    /**
     * 功能按钮授权
     *
     * @param actionId
     * @param authorityIds
     * @return
     */
    @PostMapping("/authority/action/grant")
    Result grantAuthorityAction(@RequestBody Map<String, Object> map);

    /**
     * 获取某个应用中的菜单列表
     *
     * @param appId
     * @return
     */
    @GetMapping("/authority/app/menu")
    Result<List<AuthorityMenu>> findAuthorityAppMenu(@RequestParam(value = "appId") Long appId);

    /**
     * 获取某个应用中用户可访问的菜单操作按钮权限
     * @param appId
     * @param userId
     * @return
     */
    @GetMapping("/authority/user/app/action")
    Result<List<XAuthority>> findAuthorityUserAppMenuAction(
            @RequestParam("appId") Long appId,
            @RequestParam("userId") Long userId
    );

    /**
     * 获取某个应用中角色可访问的菜单操作按钮权限获取某个应用中角色可访问的菜单操作按钮权限
     * @param appId
     * @param roleId
     * @return
     */
    @GetMapping("/authority/role/app/action")
    Result<List<XAuthority>> findAuthorityRoleAppMenuAction(
            @RequestParam(value = "roleId") Long roleId,
            @RequestParam(value = "appId") Long appId
    );

    /**
     * 获取用户所进应用的菜单权限列表
     *
     * @param appId 应用ID
     * @param userId 用户ID
     * @return
     */
    @GetMapping("/authority/user/app/menu")
    Result<List<AuthorityMenu>> findAuthorityAppMenuByUser(
            @RequestParam("userId") Long userId,
            @RequestParam("appId") String appId);
}
